package com.bs.sc.front.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bs.sc.base.utils.AjaxJson;
import com.bs.sc.front.user.dao.UserDao;
import com.bs.sc.front.user.entity.User;
import com.bs.sc.front.user.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public AjaxJson register(User user) {
		AjaxJson json = new AjaxJson();
		try {
			List<User> users = userDao.findByUserName(user.getUserName());
			if (users.size() > 0) {
				json.setCode("non-uniqueness");
				json.setMsg("用户名重复");
			} else {
				userDao.save(user);
				json.setCode("success");
				json.setMsg("注册成功");
			}
		} catch (Exception e) {
			json.setCode("failure");
			json.setMsg("系统异常");
		}
		return json;
	}

	@Override
	public List<User> findUserByUserName(String userName) {
		return userDao.findByUserName(userName);
	}

	@Override
	public List<User> findUserByUserNameAndPassword(String userName, String password) {
		return userDao.findByUserNameAndPassword(userName, password);
	}

}
